<template>
  <view class="game-result">
    <view class="result-header">
      <text class="result-title">游戏结束</text>
    </view>
    
    <view class="result-content">
      <!-- 获胜者 -->
      <view class="winner-section">
        <text class="winner-label">🎉 获胜者 🎉</text>
        <view class="winner-info">
          <image class="winner-avatar" :src="result.winner.avatar || '/static/images/profile.jpg'" mode="aspectFill"></image>
          <text class="winner-name">{{ result.winner.nickName }}</text>
        </view>
      </view>
      
      <!-- 游戏时长 -->
      <view class="duration-info">
        <text class="duration-text">游戏时长：{{ formatDuration(result.duration) }}</text>
      </view>
      
      <!-- 玩家得分列表 -->
      <view class="score-list">
        <view class="score-header">
          <text class="header-name">玩家</text>
          <text class="header-score">基础分</text>
          <text class="header-bonus">黑A</text>
          <text class="header-bonus">红A</text>
          <text class="header-total">总分</text>
        </view>
        
        <view 
          v-for="player in result.players" 
          :key="player.userId"
          class="score-item"
          :class="{ 'is-winner': player.userId === result.winner.userId }"
        >
          <view class="item-name">
            <image class="player-avatar-small" :src="player.avatar || '/static/images/profile.jpg'" mode="aspectFill"></image>
            <text>{{ player.nickName }}</text>
          </view>
          <text class="item-score" :class="[player.score > 0 ? 'positive' : player.score < 0 ? 'negative' : '']">
            {{ player.score > 0 ? '+' : '' }}{{ player.score }}
          </text>
          <text class="item-bonus" :class="[player.blackABonus > 0 ? 'positive' : player.blackABonus < 0 ? 'negative' : '']">
            {{ player.blackABonus > 0 ? '+' : '' }}{{ player.blackABonus || '-' }}
          </text>
          <text class="item-bonus" :class="[player.redABonus > 0 ? 'positive' : player.redABonus < 0 ? 'negative' : '']">
            {{ player.redABonus > 0 ? '+' : '' }}{{ player.redABonus !== 0 ? player.redABonus : '-' }}
          </text>
          <text class="item-total" :class="[player.totalScore > 0 ? 'positive' : player.totalScore < 0 ? 'negative' : '']">
            {{ player.totalScore > 0 ? '+' : '' }}{{ player.totalScore }}
          </text>
        </view>
      </view>
    </view>
    
    <view class="result-actions">
      <button class="btn-again" @click="playAgain">再来一局</button>
      <button class="btn-back" @click="backToHall">返回大厅</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      roomId: '',
      result: {
        winner: {
          userId: null,
          nickName: '',
          avatar: ''
        },
        players: [],
        duration: 0
      }
    }
  },
  
  onLoad(options) {
    this.roomId = options.roomId
    this.loadResult()
  },
  
  methods: {
    async loadResult() {
      // TODO: 从后端加载游戏结果
      // 这里暂时用mock数据
      this.result = {
        winner: {
          userId: 1,
          nickName: '张三',
          avatar: '/static/images/profile.jpg'
        },
        players: [
          {
            userId: 1,
            nickName: '张三',
            avatar: '/static/images/profile.jpg',
            score: 3,
            blackABonus: 1,
            redABonus: 0,
            totalScore: 4
          },
          {
            userId: 2,
            nickName: '李四',
            avatar: '/static/images/profile.jpg',
            score: -1,
            blackABonus: 0,
            redABonus: 0,
            totalScore: -1
          },
          {
            userId: 3,
            nickName: '王五',
            avatar: '/static/images/profile.jpg',
            score: -1,
            blackABonus: 0,
            redABonus: -1,
            totalScore: -2
          },
          {
            userId: 4,
            nickName: '赵六',
            avatar: '/static/images/profile.jpg',
            score: -1,
            blackABonus: 0,
            redABonus: 0,
            totalScore: -1
          }
        ],
        duration: 360
      }
    },
    
    // 格式化时长
    formatDuration(seconds) {
      const minutes = Math.floor(seconds / 60)
      const secs = seconds % 60
      return `${minutes}分${secs}秒`
    },
    
    // 再来一局
    playAgain() {
      uni.showModal({
        title: '提示',
        content: '是否开始新一局游戏？',
        success: (res) => {
          if (res.confirm) {
            // TODO: 调用再来一局接口
            uni.redirectTo({
              url: `/pages/game/room/index?roomId=${this.roomId}`
            })
          }
        }
      })
    },
    
    // 返回大厅
    backToHall() {
      uni.navigateTo({
        url: '/pages/game/hall/index'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.game-result {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 30rpx;
}

.result-header {
  text-align: center;
  padding: 40rpx 0;
  
  .result-title {
    font-size: 48rpx;
    font-weight: bold;
    color: #fff;
  }
}

.result-content {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16rpx;
  padding: 40rpx;
  margin-bottom: 30rpx;
}

.winner-section {
  text-align: center;
  padding-bottom: 30rpx;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 30rpx;
  
  .winner-label {
    display: block;
    font-size: 32rpx;
    font-weight: bold;
    color: #ff6b6b;
    margin-bottom: 20rpx;
  }
  
  .winner-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    
    .winner-avatar {
      width: 120rpx;
      height: 120rpx;
      border-radius: 50%;
      margin-bottom: 20rpx;
      border: 4rpx solid #ffd700;
    }
    
    .winner-name {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
    }
  }
}

.duration-info {
  text-align: center;
  margin-bottom: 30rpx;
  
  .duration-text {
    font-size: 26rpx;
    color: #999;
  }
}

.score-list {
  .score-header {
    display: flex;
    align-items: center;
    padding: 20rpx 10rpx;
    background: #f5f5f5;
    border-radius: 8rpx;
    margin-bottom: 10rpx;
    
    text {
      font-size: 24rpx;
      color: #666;
      font-weight: bold;
      text-align: center;
    }
    
    .header-name {
      flex: 2;
      text-align: left;
    }
    
    .header-score,
    .header-bonus,
    .header-total {
      flex: 1;
    }
  }
  
  .score-item {
    display: flex;
    align-items: center;
    padding: 20rpx 10rpx;
    border-bottom: 1px solid #f0f0f0;
    
    &.is-winner {
      background: #fff9e6;
    }
    
    &:last-child {
      border-bottom: none;
    }
    
    .item-name {
      flex: 2;
      display: flex;
      align-items: center;
      
      .player-avatar-small {
        width: 50rpx;
        height: 50rpx;
        border-radius: 50%;
        margin-right: 15rpx;
      }
      
      text {
        font-size: 28rpx;
        color: #333;
      }
    }
    
    .item-score,
    .item-bonus,
    .item-total {
      flex: 1;
      font-size: 28rpx;
      text-align: center;
      font-weight: bold;
      
      &.positive {
        color: #4caf50;
      }
      
      &.negative {
        color: #f44336;
      }
      
      &.zero {
        color: #999;
      }
    }
    
    .item-total {
      font-size: 32rpx;
    }
  }
}

.result-actions {
  display: flex;
  gap: 20rpx;
  
  button {
    flex: 1;
    height: 88rpx;
    line-height: 88rpx;
    border-radius: 44rpx;
    font-size: 32rpx;
    font-weight: bold;
    border: none;
  }
  
  .btn-again {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: #fff;
  }
  
  .btn-back {
    background: rgba(255, 255, 255, 0.95);
    color: #667eea;
  }
}
</style>
